package cn.tedu.tmall.admin.mall.service.impl;

import cn.tedu.tmall.admin.mall.dao.persist.repository.IGoodsRepository;
import cn.tedu.tmall.admin.mall.dao.search.GoodsElasticsearchRepository;
import cn.tedu.tmall.admin.mall.service.IGoodsService;
import cn.tedu.tmall.common.pojo.po.GoodsSearchPO;
import cn.tedu.tmall.common.pojo.vo.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Autowired
    private IGoodsRepository goodsRepository;
    @Autowired
    private GoodsElasticsearchRepository goodsElasticsearchRepository;

    @Override
    public void rebuildSearch() {
        goodsElasticsearchRepository.deleteAll();
        System.out.println("已经删除Elasticsearch中所有商品数据");

        Integer pageNum = 1;
        Integer pageSize = 3;
        Integer maxPage;
        do {
            System.out.println("正在循环读取MySQL中的商品列表数据，当前正在处理第【" + pageNum + "】页");
            PageData<GoodsSearchPO> pageData = goodsRepository.listSearch(pageNum, pageSize);
            maxPage = pageData.getMaxPage();
            List<GoodsSearchPO> list = pageData.getList();
            goodsElasticsearchRepository.saveAll(list);
            System.out.println("已经向Elasticsearch中写入第【" + pageNum + "】页的商品数据");
            pageNum++;
        } while (pageNum <= maxPage);
    }

}
